package code.tools.commponents
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class HBox extends Component
	{
		protected var _spacing:Number = 5;
		
		
		/**
		 * Constructor
		 * @param parent The parent DisplayObjectContainer on which to add this PushButton.
		 * @param xpos The x position to place this component.
		 * @param ypos The y position to place this component.
		 */
		public function HBox(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number =  0)
		{
			mouseEnabled = false;
			super(parent, xpos, ypos);
		}
		
		/**
		 *  
		 * 移除全部子显示对象
		 */		
		public function removeAll():void
		{
			while(this.numChildren)
				this.removeChildAt(0);
		}
		
		/**
		 * Override of addChild to force layout;
		 */
		override public function addChild(child:DisplayObject) : DisplayObject
		{
			super.addChild(child);
			child.addEventListener(Event.RESIZE, onResize);
			invalidate();
			return child;
		}
		
		protected function onResize(event:Event):void
		{
			invalidate();
		}
		
		/**
		 * Draws the visual ui of the component, in this case, laying out the sub components.
		 */
		override public function draw() : void
		{
			_width = 0;
			_height = 0;
			var xpos:int = 0;
			_visibleNumChildren = 0;
			
			//			var left:Number;
			for(var i:int = 0; i < numChildren; i++)
			{
				var child:DisplayObject = getChildAt(i);
				if(!child.visible) continue; /* ignore hidden children */
				//				var rect:Rectangle = child.getRect(child);
				//				if(!isNaN(left)){
				//					xpos += left - rect.left;
				//				}
				child.x = xpos;
				xpos += child.width;
				xpos += _spacing;
				_width += child.width + _spacing;
				
				//				left = rect.left;
				
				_height = Math.max(_height, child.height);
				_visibleNumChildren++;
			}
			_width -= _spacing;
			//			_width += _spacing * (numChildren - 1);
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		/**
		 *返回可见的数量
		 * @return 
		 * 
		 */		
		public function get visibleNumChildren():int
		{
			return _visibleNumChildren;
		}
		private var _visibleNumChildren:int;
		/**
		 * Gets / sets the spacing between each sub component.
		 */
		public function set spacing(s:Number):void
		{
			_spacing = s;
			invalidate();
		}
		public function get spacing():Number
		{
			return _spacing;
		}
	}
}